package jp.sourceforge.talisman.mds.ui;

/*
 * $Id: MdsGraphSetting.java,v 1.1 2009/09/12 05:06:21 weiwei Exp $
 */

import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Haruaki Tamada
 * @version $Revision: 1.1 $ 
 */
public class MdsGraphSetting{
    private static final int TRANSPARENCY = 200;

    private Color foreground = Color.BLACK;
    private Color background = Color.WHITE;
    private Color gridColor = Color.DARK_GRAY;
    private Color subGridColor = Color.GRAY;
    private Color selectedAreaColor = Color.LIGHT_GRAY;
    private boolean showLabels = true;
    private boolean showGrid = true;
    private boolean showSubGrid = true;
    private boolean selectable = true;

    private List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();

    /**
     * default constructor.
     */
    public MdsGraphSetting(){
        setForeground(Color.BLACK);
        setBackground(Color.WHITE);
        setGridColor(Color.DARK_GRAY);
        setSubGridColor(Color.GRAY);
        setSelectedAreaColor(Color.LIGHT_GRAY);
        setShowLabels(true);
        setShowGrid(true);
        setShowSubGrid(true);
        setSelectable(true);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener){
        listeners.add(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener){
        listeners.remove(listener);
    }

    protected void fireEvent(PropertyChangeEvent e){
        for(PropertyChangeListener listener: listeners){
            listener.propertyChange(e);
        }
    }

    public Color getGridColor(){
        return gridColor;
    }

    public Color getSubGridColor(){
        return subGridColor;
    }

    public Color getSelectedAreaColor(){
        return selectedAreaColor;
    }

    public Color getBackground(){
        return background;
    }

    public Color getForeground(){
        return foreground;
    }

    public boolean isShowLabels(){
        return showLabels;
    }

    public boolean isShowGrid(){
        return showGrid;
    }

    public boolean isShowSubGrid(){
        return showSubGrid;
    }

    public boolean isSelectable(){
        return selectable;
    }

    public void setGridColor(Color gridColor){
        if(gridColor == null){
            throw new NullPointerException();
        }
        Color oldColor = this.gridColor;
        this.gridColor = gridColor;
        fireEvent(new PropertyChangeEvent(this, "grid_color", oldColor, gridColor));
    }

    public void setSubGridColor(Color color){
        if(color == null){
            throw new NullPointerException();
        }
        Color oldColor = this.subGridColor;
        this.subGridColor = color;
        fireEvent(new PropertyChangeEvent(this, "subgrid_color", oldColor, color));
    }

    public void setBackground(Color background){
        if(background == null){
            throw new NullPointerException();
        }
        Color oldColor = this.background;
        this.background = background;
        fireEvent(new PropertyChangeEvent(this, "background", oldColor, background));
    }

    public void setForeground(Color foreground){
        if(foreground == null){
            throw new NullPointerException();
        }
        Color oldColor = this.foreground;
        this.foreground = foreground;
        fireEvent(new PropertyChangeEvent(this, "foreground", oldColor, foreground));
    }

    public void setShowLabels(boolean showLabels){
        boolean old = this.showLabels;
        this.showLabels = showLabels;
        fireEvent(new PropertyChangeEvent(
            this, "show_labels", 
            new Boolean(old), new Boolean(showLabels))
        );
    }

    public void setShowGrid(boolean showGrid){
        boolean old = this.showGrid;
        this.showGrid = showGrid;
        fireEvent(new PropertyChangeEvent(
            this, "show_grid",
            new Boolean(old), new Boolean(showGrid))
        );
    }

    public void setShowSubGrid(boolean showSubGrid){
        boolean old = this.showSubGrid;
        this.showSubGrid = showSubGrid;
        fireEvent(new PropertyChangeEvent(
            this, "show_subgrid",
            new Boolean(old), new Boolean(showSubGrid))
        );
    }

    public void setSelectable(boolean selectable){
        boolean old = this.selectable;
        this.selectable = selectable;
        fireEvent(new PropertyChangeEvent(
            this, "selectable",
            new Boolean(old), new Boolean(selectable))
        );
    }

    public void setSelectedAreaColor(Color color){
        if(color == null){
            throw new NullPointerException();
        }
        Color old = this.selectedAreaColor;
        this.selectedAreaColor = new Color(color.getRed(), color.getGreen(), color.getBlue(), TRANSPARENCY);
        fireEvent(new PropertyChangeEvent(
            this, "selected_area_color", old, color
        ));
    }
}

